package com.lemon.smartsafety.ui.activity.address;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.address.AddrBookUserVo;
import com.lemon.smartsafety.entity.event.address.AddressRefreshEvent;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.service.BaseHttpServices;
import com.lemon.smartsafety.result.BaseResult;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.utils.app.ToastUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WXSMSAcitivity extends SwipeBackActivity {


    @Bind(R.id.txt_name)
    TextView txtName;
    @Bind(R.id.edt_title)
    EditText edtTitle;
    @Bind(R.id.edt_content)
    EditText edtContent;
    @Bind(R.id.btn_save)
    Button btnSave;

    private List<AddrBookUserVo> mCheckDataList = new ArrayList<>();
    private String strMobile = "";
    private String strName = "";

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            mCheckDataList = (List<AddrBookUserVo>) getIntent().getSerializableExtra(BundleKey.ADDRESS_BOOK_WX);
        }

        for (AddrBookUserVo addrBookUserVo : mCheckDataList) {
            if (CommonUtil.strIsEmpty(strMobile)) {
                strMobile = addrBookUserVo.getMobile();
                strName = addrBookUserVo.getUser_name();
            } else {
                strMobile = strMobile + "," + addrBookUserVo.getMobile();
                strName = strName + "," + addrBookUserVo.getUser_name();
            }
        }


    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_wx_sms;
    }

    @Override
    protected void initTitle() {
        setBarTitle("通知发送界面");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(WXSMSAcitivity.class);
    }

    @Override
    protected void initViews() {
        txtName.setText(strName);
        initToolBar();

    }


    @Subscribe
    public void onEventMainThread(final AddressRefreshEvent event) {

    }


    @OnClick({R.id.btn_save})
    public void onButtonClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                httpSendWxSMS();
                break;
            default:
                break;
        }
    }


    private void httpSendWxSMS() {

        ToastUtil.init(this);
        if (CommonUtil.strIsEmpty(edtTitle.getText().toString().trim())) {
            ToastUtil.show("请填写标题");
            return;
        }

        if (CommonUtil.strIsEmpty(edtContent.getText().toString().trim())) {
            ToastUtil.show("请填写内容");
            return;
        }


        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("op", "send_weixin_message");
        params.put("mobile", strMobile);
        params.put("title", edtTitle.getText().toString().trim());
        params.put("content", edtContent.getText().toString().trim());

        BaseHttpServices baseHttpServices = new BaseHttpServices(this);

        HttpOkBase.httpBasePost(params, baseHttpServices.mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                if (httpClient) {
                    try {
                        BaseResult baseResult = new Gson().fromJson(result, BaseResult.class);
                        if (!CommonUtil.objIsEmpty(baseResult)) {
                            if (!baseResult.isIsError()) {
                                ToastUtils.toastMsg(baseResult.getError().getMsg());
                                finish();
                            } else {
                                mHUDUtil.showErrorTip(baseResult.getError().getMsg());
                                LoggerHelper.error(getClass(), result);
                            }
                        }
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
